﻿module Sustainalytics.Tools.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;    
    import HttpPromise = ng.IHttpPromise;

    export class ReportFTPService implements IReportFTPService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;
        private $location: ng.ILocationService;

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects,
            $location) {

            var ctx = this;

            ctx.$q = $q;
            ctx.$http = $http;
            ctx.notificationFactory = notificationFactory;
            ctx.staticObjectsAdmin = staticObjectsAdmin;
            ctx.$location = $location;
        }

        public loadReportFtpForAccountId(accountId: string): ng.IPromise<ReportFTPDto> {
            var ctx = this;
            var def = this.$q.defer();

            var promise = ctx.$http.get('/reportftpserv/api/v1/ftpaccountinfo/account/' + accountId);

            promise
                .success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                def.reject({ data: data, status: status, headers: headers, config: config });
            });

            return def.promise;
        }

        private servUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() +
                "/reportftpserv/api/v1/ftpaccountinfo/";
        }

        public createReportFtp(dto: ReportFTPDto): ng.IHttpPromise<ReportFTPDto> {
            var ctx = this;

            var promise = ctx.$http.post<ReportFTPDto>(ctx.servUrl(), dto);
            return promise;
        }

        public updateReportFtp(dto: ReportFTPDto): ng.IHttpPromise<ReportFTPDto> {
            var ctx = this;

            var promise = ctx.$http.put<ReportFTPDto>(ctx.servUrl(), dto);

            return promise;
        }

        public deleteReportFtp(repId: string): ng.IHttpPromise<any> {
            var ctx = this;
            var url: string = '/reportftpserv/api/v1/ftpaccountinfo/reportId/' + repId;
            var promise = ctx.$http.delete(url);
            return promise;
        }
    }

    // register service with Tools Angular module
    var mod = angular.module(toolsAdminModuleName);

    mod.service('ReportFTPService',
        ['$q', '$http', 'notificationFactory', 'staticObjectsAdmin', '$location', ReportFTPService]);
} 